/*!
 * Component: components/Index/index
 * License: MIT
 * Created: 2019-09-28 11:51
 */

import { getServer } from '/store/mutations'
import { GlobalData } from '/store/state'

Component({
  /**
   * Component properties
   */
  properties: {
    commodity: Object,
    currencyname: String,
  },

  /**
   * Component initial data [private]
   */
  data: {
    access: GlobalData.access,
    hasVideo: false,
    isMine: false,
  },

  observers: {
    commodity({ photos = [], user_id }) {
      let hasVideo = false
      for (let i = 0; i < photos.length; i++) {
        if (photos[i].video) {
          hasVideo = true
          break
        }
      }
      const user = wx.getStorageSync(`${getServer()}/logged`).data

      this.setData({
        hasVideo,
        isMine: user.id === user_id,
      })
    },
  },

  /**
   * lifecycle of component
   */
  lifetimes: {
    attached() {
      this.setData({
        access: GlobalData.access,
      })
    },
  },

  /**
   * Component methods
   */
  methods: {
    toDetail(e: any) {
      const {
        currentTarget: {
          dataset: { commodityid },
        },
      } = e

      wx.navigateTo({
        url: `/mall/detail/detail?commodityId=${commodityid}`,
      })
    },
  },
})
